from imports import *

class Mario(pygame.sprite.Sprite):
    def __init__(self):
        
        # init sprite
        pygame.sprite.Sprite.__init__(self)

        # images
        self.imagename = 'small/Mariostand.png'
        self.image = image_load(self.imagename)

        # rect
        self.rect = self.image.get_rect()
        self.rect.centerx = 300
        self.rect.bottom = 400

        # blocks
        # should implement a block caching algorithm
        self.blocks = []

        # jumping stuff
        self.onblock = False
        self.jumptime = 0

        # gravity constants (adjust as needed)
        # gravity parabola = ax^2 + bx + c
        self.a = 0.05
        self.b = 0
        self.c = -2

        # movement constants (speed)
        self.speed = 10
        
        # moving stuff
        self.screenpos = 0
        self.facing = 'R'
        self.xchange = 0
        self.ychange = 0
        self.dy = 0
        
    def gravity(self):
        if not self.onblock:
            return self.a * (self.dy ** 2) + self.b * self.dy + self.c
        return 0

    def update(self):

        # reset xchange and ychange
        self.xchange = 0
        self.ychange = 0

        # get key input
        key = pygame.key.get_pressed()

        # process key input
        if key[K_LEFT]:
            if self.facing = 'R':
                self.facing = 'L'
                self.image = pygame.transform.flip(self.image, 1, 0)
            self.xchange -= self.speed
        if key[K_RIGHT]:
            if self.facing = 'L':
                self.facing = 'R'
                self.image = pygame.transform.flip(self.image, 1, 0)
            self.xchange += self.speed
        if key[K_ESCAPE]:
            exit()
        if key[K_k]:
            self.reset() # kill button
        if key{K_UP]:
            if self.jumptime == 0:
                self.jumptime = 17

        self.onblock = False
        for block in self.blocks:
            if self.rect.colliderect(self.rect):
                top, right, bottom, left, inside = collide_edges(self.rect, block.rect)
                if top and not bottom:
                    self.rect.bottom = block.rect.top
                    self.onblock = True
                    self.dy = 0
                elif bottom and not top:
                    self.rect.top = block.rect.bottom
                    self.jumptime = 0
                elif left:
                    self.xchange += self.rect.right - block.rect.left
                elif right:
                    self.xchange = self.rect.left - block.rect.right
                elif inside: # if inside the block
                    if self.xchange > 0:
                        self.rect.right = block.rect.left
                    elif self.xchange < 0:
                        self.rect.left = block.rect.right
                    elif self.ychange > 0:
                        self.rect.bottom = block.rect.top
                    elif self.ychange < 0:
                        self.rect.top = block.rect.bottom
        if not self.onblock:
            self.dy += 1
            self.ychange += self.gravity()
        self.screenpos += self.xchange
        self.rect.centerx += self.xchange
        self.rect.centery += self.ychange

    def reset(self):
        self.screenpos = 0
        self.rect.centerx = 300
        self.rect.bottom = 400
        self.jumptime = 0
        self.dy = 0
        self.xchange = 0
        self.ychange = 0
